---
title: "Power Analysis"
author: "Kaylyn Jackson Schiff, Daniel Schiff, and Natalia Bueno"
date: "2020"
output: pdf_document
editor_options: 
  chunk_output_type: console
---

#####Setup Chunk
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(rmarkdown)
library(tidyverse)
library(estimatr)
options(scipen=999)
```


####MDE
```{r mde}
load("data/pilot_clean.RData")
#Get SDs of outcome variables
pilot_SDs <- pilot %>% group_by(alleg_treatment) %>% 
  summarize(belief_SD = sd(alleg_believe),  #connects to belief outcome on ultimate survey 
  rate_SD = sd(alleg_rate),                 #connects to support and trust outcomes on ultimate survey
  uncertainty_SD = sd(alleg_confident))     #connects to uncertainty in belief on ultimate survey

n <- seq(from=1000, to=6000, by=250)

belief_MDE <- vector()
support_MDE <- vector()
uncertainty_MDE <- vector()

for (i in 1:length(n)){
belief_MDE[i] <- 2.8 * sqrt((pilot_SDs[1,2]^2/(n[i]/3)) + (pilot_SDs[2,2]^2/(n[i]/3)) + (pilot_SDs[3,2]^2/(n[i]/3)))
support_MDE[i] <- 2.8 * sqrt((pilot_SDs[1,3]^2/(n[i]/3)) + (pilot_SDs[2,3]^2/(n[i]/3)) + (pilot_SDs[3,3]^2/(n[i]/3)))
uncertainty_MDE[i] <- 2.8 * sqrt((pilot_SDs[1,4]^2/(n[i]/3)) + (pilot_SDs[2,4]^2/(n[i]/3)) + (pilot_SDs[3,4]^2/(n[i]/3)))
}

#Divide MDEs by control outcome SDs so they are standardized
belief_MDE <- unlist(belief_MDE)/pull(pilot_SDs[1,2])
support_MDE <- unlist(support_MDE)/pull(pilot_SDs[1,3])
uncertainty_MDE <- unlist(uncertainty_MDE)/pull(pilot_SDs[1,4])

#Plot
png("Figures/mde_calculations.png")
mde_data <- as.data.frame(cbind(n, belief_MDE, uncertainty_MDE, support_MDE))
support_mde_plot <- ggplot(mde_data, aes(x=n, y=support_MDE)) + geom_point() +
  xlab("Sample Size") + ylab("MDE (Standardized)\n") + ggtitle("MDE by Sample Size: Support Outcome") +
  scale_y_continuous(limits = c(.1,.3), breaks = seq(.05, .3, by = .05)) +
  scale_x_continuous(limits = c(1000,6000), breaks = seq(1000,6000, by = 1000)) +
  theme_bw()
support_mde_plot
dev.off()

#ballpark, for sds of about 1.5 and MDEs of .3 to .4: this translates to standardized effects of about .2 to .266, and would require sample sizes of ~1000 to ~1750 to pick up and be sufficiently powered
```


